package com.bw.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.domain.SysMenu;
import com.bw.mapper.SysRoleMenuMapper;
import com.bw.service.SysMenuService;
import com.bw.mapper.SysMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author THINKAPD
* @description 针对表【sys_menu】的数据库操作Service实现
* @createDate 2024-03-04 15:28:13
*/
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu>
    implements SysMenuService{
    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Autowired
    SysRoleMenuMapper sysRoleMenuMapper;

    @Transactional
    @Override
    public boolean delById(Integer menuId) {
        try {
            // （16）菜单级联删除功能，考虑多表操作和事务4分

            //0. 先根据菜单id查询出所有涉及的菜单ID --比如该菜单ID可能是父级菜单,就需要查询出他所有子菜单,包含自己
            List<Integer> mids= sysMenuMapper.getAllMids(menuId);

            // 2 先删除中间表
            for (Integer mid : mids) {
                int i= sysRoleMenuMapper.deleteByMid(mid);
            }

            //3.删除菜单表
            int i= sysMenuMapper.delById(menuId);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("删除菜单失败");
        }


    }

    @Override
    public List<SysMenu> listByUserId(String userId) {
        return sysMenuMapper.listByUserId(userId);
    }
}




